﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using org.in2bits.MyXls;

namespace ZDMHelper
{
    class ExcelOperations
    {
        private XlsDocument xls;
        private Workbook book;

        public delegate void ErrorHandler(string message);
        public event ErrorHandler errorHandler;

        public ExcelOperations(string fileName)
        {
            try
            {
                this.xls = new XlsDocument(fileName);
                this.book = xls.Workbook;
            }
            catch
            {
                //errorHandler("Excel文件出于打开状态，请关闭后重试！");
            }
        }

        /// <summary>
        /// 读取断链数据
        /// </summary>
        /// <returns>排序后的断链集合</returns>
        public List<ChainScission> ReadChainScissions()
        {
            List<ChainScission> chains = new List<ChainScission>();
            if (IsSheetExist(xls, "断链"))
            {
                Worksheet sheet = this.book.Worksheets["断链"];
                try
                {
                    //第一行为标题栏，myxls行集合rows、列从1开始
                    for (ushort i = 2; i < sheet.Rows.Count; i++)
                    {
                        string fs = sheet.Rows[i].CellAtCol(1).Value.ToString();
                        double fm = Double.Parse(sheet.Rows[i].CellAtCol(2).Value.ToString());
                        string bs = sheet.Rows[i].CellAtCol(3).Value.ToString();
                        double bm = Double.Parse(sheet.Rows[i].CellAtCol(4).Value.ToString());
                        ChainScission chain = new ChainScission(fs, fm, bs, bm);
                        chains.Add(chain);
                    }
                }
                catch
                {
                    errorHandler("读取断链表错误，请检查数据格式！");
                }
            }
            else
            {
                errorHandler("文件不存在断链表，请检查数据文件！");
            }
            chains.Sort();
            return chains;
        }

        /// <summary>
        /// 读取标尺数据
        /// </summary>
        /// <returns>排序后的标尺集合</returns>
        public List<BrokenHeight> ReadBrokenHeights()
        {
            List<BrokenHeight> heights = new List<BrokenHeight>();
            if (IsSheetExist(xls, "标尺"))
            {
                Worksheet sheet = this.book.Worksheets["标尺"];
                try
                {
                    //第一行为标题栏，myxls行集合rows、列从1开始
                    for (ushort i = 2; i < sheet.Rows.Count; i++)
                    {
                        string s = sheet.Rows[i].CellAtCol(1).Value.ToString();
                        double m = Double.Parse(sheet.Rows[i].CellAtCol(2).Value.ToString());
                        double h = Double.Parse(sheet.Rows[i].CellAtCol(3).Value.ToString());
                        BrokenHeight height = new BrokenHeight(s, m, h);
                        heights.Add(height);
                    }
                }
                catch
                {
                    errorHandler("读取标尺表错误，请检查数据格式！");
                }
            }
            else
            {
                errorHandler("文件不存在标尺表，请检查数据文件！");
            }
            heights.Sort();
            return heights;
        }

        /// <summary>
        /// 读取控制标高数据
        /// </summary>
        /// <returns>控制标高集合</returns>
        public List<ElevationData> ReadElevationDatas()
        {
            List<ElevationData> datas = new List<ElevationData>();
            if (IsSheetExist(xls, "控制标高"))
            {
                Worksheet sheet = this.book.Worksheets["控制标高"];
                try
                {
                    //第一行为标题栏，myxls行集合rows、列从1开始
                    for (ushort i = 2; i < sheet.Rows.Count; i++)
                    {
                        string s = sheet.Rows[i].CellAtCol(1).Value.ToString();
                        double m = Double.Parse(sheet.Rows[i].CellAtCol(2).Value.ToString());
                        double be = Double.Parse(sheet.Rows[i].CellAtCol(3).Value.ToString());
                        double ce = Double.Parse(sheet.Rows[i].CellAtCol(4).Value.ToString());
                        string f = sheet.Rows[i].CellAtCol(5).Value.ToString();
                        Flag flag;
                        if (f == "0")
                        {
                            flag = Flag.Rivers;
                        }
                        else if (f == "1")
                        {
                            flag = Flag.Load;
                        }
                        else
                        {
                            flag = Flag.Null;
                        }
                        ElevationData data = new ElevationData(s, m, ce, be, flag);
                        datas.Add(data);
                    }
                }
                catch
                {
                    errorHandler("读取控制标高表错误，请检查数据格式！");
                }
            }
            else
            {
                errorHandler("文件不存在控制标高表，请检查数据文件！");
            }
            return datas;
        }

        /// <summary>
        /// 读取行政区划数据
        /// </summary>
        /// <returns>行政区划数据集合</returns>
        public List<RegionData> ReadRegionDatas()
        {
            List<RegionData> datas = new List<RegionData>();
            if (IsSheetExist(xls, "行政区划"))
            {
                Worksheet sheet = this.book.Worksheets["行政区划"];
                try
                {
                    //第一行为标题栏，myxls行集合rows、列从1开始
                    for (ushort i = 2; i < sheet.Rows.Count; i++)
                    {
                        string s = sheet.Rows[i].CellAtCol(1).Value.ToString();
                        double m = Double.Parse(sheet.Rows[i].CellAtCol(2).Value.ToString());
                        string fr = sheet.Rows[i].CellAtCol(3).Value.ToString();
                        string br = sheet.Rows[i].CellAtCol(4).Value.ToString();
                        RegionData data = new RegionData(s, m, fr, br);
                        datas.Add(data);
                    }
                }
                catch
                {
                    errorHandler("读取行政区划表错误，请检查数据格式！");
                }
            }
            else
            {
                errorHandler("文件不存在行政区划表表，请检查数据文件！");
            }
            return datas;
        }

        private bool IsSheetExist(XlsDocument xls, string sheetName)
        {
            Workbook wb = xls.Workbook;
            for (int i = 0; i < wb.Worksheets.Count; i++)
            {
                if (wb.Worksheets[i].Name == sheetName)
                {
                    return true;
                }
            }
            return false;
        }
    }
}
